<?php

namespace app\aiv110\controller;

use app\aiv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\aiv110\model\StoreApply;
use app\aiv110\model\StoreCtm;
use app\aiv110\model\Notice;
use app\aiv110\model\Custom;
use app\aiv110\model\Store;

class StoreController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 50000  您已经申请过了，请耐心等待
         * 50001 
         * 50002  
         * 50003
         * 50004
         */
    }

    /*
     * 店铺加盟申请
     * 日期 2019-10-17
     */

    public function storeApply() {
        $rule = [
            'name' => 'require',
            'phone' => 'require|length:11',
//            'prov' => 'require',
//            'city' => 'require',
            'type' => 'require',
        ];
        $msg = [
            'name.require' => '姓名必须',
            'phone.require' => '请输入手机号',
            'phone.length' => '手机号长度为11',
//            'prov.require' => '请输入省份',
//            'city.require' => '请输入城市',
            'type.require' => '来源必须', //1app2百度
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $name = $all['name'];
        $phone = $all['phone'];
        $prov = input('param.prov');
        $city = input('param.city');
        $type = $all['type'];

        $storeApplyRow = StoreApply::where('phone', $phone)->find();
        if ($storeApplyRow) {
            return Utils::ajaxReturn(50000, '您已经申请过了，请耐心等待', null);
        }

        //写入数据
        $storeApplyResult = StoreApply::insert([
                    'name' => $name,
                    'phone' => $phone,
                    'prov' => $prov,
                    'city' => $city,
                    'type' => $type,
                    'add_time' => date('Y-m-d H:i:s'),
        ]);
        if ($storeApplyResult) {
            return Utils::ajaxReturn(0, '申请成功', null);
        }
    }

    /*
     * 记录用户浏览店铺
     * 日期 2020-03-24
     */

    public function ctmBrowseStore() {
        $rule = [
            'ctm_id' => 'require',
            'staff_id' => 'require',
            'store_id' => 'require',
            'type' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'staff_id.require' => '美容师id必须',
            'store_id.require' => '店铺id必须',
            'type.require' => 'type必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $staff_id = $all['staff_id'];
        $store_id = $all['store_id'];
        $type = $all['type'];

        //写入数据
        $storeCtmResult = StoreCtm::insert([
                    'type' => $type,
                    'store_id' => $store_id,
                    'staff_id' => $staff_id,
                    'ctm_id' => $ctm_id,
                    'add_time' => date('Y-m-d H:i:s'),
        ]);
        $ctmNum = StoreCtm::where('ctm_id', $ctm_id)->where('store_id', $store_id)->count();
        if ($ctmNum == 1) {
            $ApiPath = config('appconfig.ApiPath8890');
            $store_keeper = Store::where('id', $store_id)->value('store_keeper');
            $postFields['content'] = '【新客】尊敬的' . $store_keeper . '，有新客户到店啦';
            $postFields['type'] = 100; //0新店上新7提现
            $postFields['titleId'] = 1; //1店铺动态2预约消息3营收消息keeper_phone
            $postFields['otherId'] = $ctm_id; //新店上新就是0
            $postFields['staffId'] = 0;
            $postFields['storeId'] = $store_id;
            $postFields['phone'] = Store::where('id', $store_id)->value('keeper_phone');
            $relust = Utils::curlPost($ApiPath . 'push/pushMessage/', json_encode($postFields));
        }
        if ($storeCtmResult) {
            return Utils::ajaxReturn(0, '添加成功', null);
        }
    }

    /*
     * 公告详情
     * 日期 2020-05-18
     */

    public function noticeInfo() {
        $id = input('param.id');
        $row = Notice::where('id', $id)->find();
        return Utils::ajaxReturn(0, '查询成功', $row);
    }

    /*
     * 定时发送店铺营业短信
     * 日期 2021-05-24
     * 每天早上7.30执行任务
     */

    public function sendStoreOpenSms() {
        //当天开始时间
        $begintime = date("Y-m-d H:i:s", mktime(0, 0, 0, date('m'), date('d'), date('Y')));
        //当天结束时间
        $endtime = date("Y-m-d H:i:s", mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1);
        //查询今天开始营业的店铺
        $list = Db::name('store')
                ->where('is_del', 1)
                ->where('auditstore', 1)
                ->where('open_time', 'between', [$begintime, $endtime])
                ->where('is_sendsms',2)
                ->field('id,open_time,store_name')
                ->select();
        foreach ($list as $k => $v) {
            $store_name = $v['store_name'];
            $store_id = $v['id'];
            $content = "尊敬的顾客，DAVIDKKM无人美容店-" . $store_name . "今日盛大开业。登录微信小程序【DAVID KKM】，新客专享项目39.9。退订回T";
            if($store_id == 93){
                $content = "DAVIDKKM会纺路店10月17日盛大开业，开业当天进店抽红包，全场无空奖，享全年补水项目，董晓霜携全员欢迎您光临!";
            }
            $telephone = StoreCtm::alias('s')
                    ->join('custom c', 'c.id = s.ctm_id', 'left')
                    ->where('s.store_id', $store_id)
                    ->group('s.ctm_id')
                    ->column('c.custom_phone');
            $telephone = implode(",", $telephone);
            $sendResult = Utils::sendSemSms($telephone, $content);
            Store::where('id',$store_id)->update(['is_sendsms'=>1]);
        }
    }

}
